/**
 * Created by John on 2017/1/20.
 */
import { Component } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/switchMap';

import { editGameService } from '../../service/editGame.service';

declare let $:any;

@Component({
    selector: 'edit_game',
    templateUrl: '../../../views/edit_game/editGame.html',
    styleUrls: ['../../../css/edit_game/editGame.css'],
    providers: [
        editGameService
    ]
})
export class editGameComponent {
    public resource_url:any;
    private gid : any;  //项目ID
    public isChoose:any = false;
    public description:any;  //项目描述
    public marketing_mode:any = 1;  //营销方式
    public rule:any;  //活动规则
    public title:any;  //项目名称
    public userId:any = $.cookie("id");  //用户ID
    public app_id:any;
    public command:any;  //红包口令
    public max_price:any;  //最大金额
    public min_price:any; //最小金额
    public price:any;  //总金额
    public total:any; //红包个数
    public win_odds:any; //获奖概率
    public win_value:any; //赢取奖励分数
    public win_where:any; //赢取奖励条件
    public wishing:any; //祝福语

    public zip:any;
    public zip_link:any;
    public resource:any;
    public id:any;  //小游戏编号
    public projectData:any = {};

    public formData:FormData;
    public fileName:any;

    constructor(
        private router : Router,
        private route: ActivatedRoute,
        private edit:editGameService,
        private http: Http
    ){
        let self : any = this;
        console.log( self.route.params.value.id);
        console.log( self.userId);
        self.gid = self.route.params.value.id;
        self.getGameSource();
    }

    // 得到游戏资源
    public getGameSource(){
            let self:any = this;
            let data:any = {
                id : self.gid
            };
            self.edit.getSource({
                data: data,
                success: function(result:any){
                    console.log(result);
                    self.resource_url = result.resource_url;
                    self.zip_link = result.zip_link;
                    self.audio = result.audio;
                    self.projectData = result;

                    self.resource = result.resource;

                    self.title = result.title;
                    self.description = result.description;
                    self.rule = result.rule;
                    self.max_price = result.max_price;
                    self.min_price = result.min_price;
                    self.command = result.command;
                    self.total = result.total;
                    self.price = result.price;
                    self.win_odds = result.win_odds;
                    self.win_value = result.win_value;
                    self.win_where = result.win_where;
                    switch (self.win_where){
                        case 0:
                            $("#winChange").find("option[value = 0]").attr("selected",true);
                            break;
                        case 1:
                            $("#winChange").find("option[value = 1]").attr("selected",true);
                            console.log(self.win_where);
                            break;
                        case 2:
                            $("#winChange").find("option[value = 2]").attr("selected",true);
                            console.log(self.win_where);
                            break;
                        case 3:
                            $("#winChange").find("option[value = 3]").attr("selected",true);
                            console.log(self.win_where);
                            break;
                    }
                    self.wishing = result.wishing;
                    if (result.status == 1){
                        $("#winChange").attr("disabled",true);
                        $("input[name=total]").attr("disabled",true);
                        $("input[name=win_value]").attr("disabled",true);
                        $("input[name=price]").attr("disabled",true);
                        $("input[name=win_odds]").attr("disabled",true);
                        $("input[name=min_price]").attr("disabled",true);
                        $("input[name=max_price]").attr("disabled",true);
                        $("input[name=command]").attr("disabled",true);
                        $("input[name=wishing]").attr("disabled",true);
                    }
                }
            })
    }

    public changeWin(){
        this.win_where = $("#winChange").val();
        console.log(this.win_where);
    }

    public ischoose1(){
        this.isChoose = false;
        this.marketing_mode = 1;
        $('.b1').addClass('bg');
        $('.b2').removeClass('bg');
        $('.b1 a:nth-child(1)').css('display','block');
        $('.b1 a:nth-child(2)').css('display','none');
        $('.b2 a:nth-child(1)').css('display','block');
        $('.b2 a:nth-child(2)').css('display','none');
    }
    public ischoose2(){
        this.isChoose = true;
        this.marketing_mode = 0;
        $('.b2').addClass('bg');
        $('.b1').removeClass('bg');
        $('.b1 a:nth-child(2)').css('display','block');
        $('.b1 a:nth-child(1)').css('display','none');
        $('.b2 a:nth-child(2)').css('display','block');
        $('.b2 a:nth-child(1)').css('display','none');
    }

    // 上传资源
    onFileChanged(fileList: FileList) {
        let self:any = this;
        if (fileList.length > 0) {
            let file: File = fileList[0];
            let name:any = file.name.split(".")[1];
            if (name == "zip"){
                self.fileName = file.name;
                self.formData = new FormData();
                self.formData.append('zip', file, file.name);
            }else {
                alert("请选择以zip为格式的压缩文件");
            }

        }
    }
    public submit(){
        let self:any = this;
        let headers = new Headers({
            "Accept": "application/json"
        });
        let options = new RequestOptions({ headers });
        this.http.post("http://api.90838.com/wechat/mp/game/source/upload", self.formData , options)
            .map(res => res.json())
            .subscribe(
                data => {
                    console.log('success' + data);
                    self.rellySend();
                },
                error => console.log(error)
            )
    }
    public rellySend(){
        let self:any = this;
        let member_id = $.cookie("id");
        let token = $.cookie("token");
        let data:any = {
            id : self.gid,
            member_id : member_id,
            token : token
        };
        console.log(data);
        self.edit.updateSource({
            data:data,
            success:function (result:any) {
                console.log(result);
                if(result.error === 0){
                    self.fileName = "";
                    alert("上传成功");
                }
            }
        })
    }
    // 设置项目
    public setProject(){
        let self:any = this;
        let data:any = {
            description : self.description,
            id : self.gid,
            marketing_mode : self.marketing_mode,
            member_id : self.userId,
            rule : self.rule,
            title : self.title,
            app_id : self.app_id,
            //不是必填项
            command : self.command,
            max_price : self.max_price,
            min_price : self.min_price,
            price : self.price,
            total : self.total,
            win_odds : self.win_odds,
            win_value : self.win_value,
            win_where : self.win_where,
            wishing : self.wishing
        };
        console.log(data);
        self.edit.projectSet({
            data: data,
            success: function(result:any){
                console.log(result);
                if(result.error === 0){
                    alert("修改成功");
                    self.getGameSource();
                }else {
                    alert(result.msg);
                }
            }
        })
    }

}
